/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.PriorityObjectDAL;
import mnstudent.datatranferobject.PriorityObjectDTO;
import mnstudent.global.Standard;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author 11520_000
 */
public class PriorityObjectBLL {
    
    private PriorityObjectDAL m_priorityObjDAL;
    
    public PriorityObjectBLL()
    {
        m_priorityObjDAL = new PriorityObjectDAL();
    }
    
      /**
     * Ham lay tat ca cac Doi tuong
     *
     * @return boolean
     */
    public ResultSet getAllPriorityObject() {
      
        return m_priorityObjDAL.getAllPriorityObject();
    }
    
    public ArrayList<PriorityObjectDTO> getAllPriorityObjectToArray()
    {
        ArrayList<PriorityObjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllPriorityObject();
        try {
            while(rs.next())
            {
                PriorityObjectDTO state = new PriorityObjectDTO(rs.getNString(1), rs.getNString(2), rs.getInt(3));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
     /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getPriorityObjectById(String _id)
    {
        return m_priorityObjDAL.getPriorityObjectById(_id);
    }
    
     /**
     * ham them moi khoa xuong database
     * @param _priorityObj
     * @return true: success, false: fail
     */
    public boolean insertPriorityObjectToDatabase(PriorityObjectDTO _priorityObj)
    {
       return m_priorityObjDAL.insertPriorityObjectToDatabase(_priorityObj);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _major
     * @return tue: thanh cong, false: that bai
     */
    public boolean updatePriorityObjectToDatabase(PriorityObjectDTO _priorityObj)
    {
      
        return m_priorityObjDAL.updatePriorityObjectToDatabase(_priorityObj);
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _major
     * @return 
     */
    public boolean deletePriorityObjectFromDatabase(PriorityObjectDTO _priorityObj)
    {
        return m_priorityObjDAL.deletePriorityObjectFromDatabase(_priorityObj);
    }
    
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
    //</editor-fold>
}
